package com.flot.yazb.widget.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.manager.RequestManagerRetriever;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by jinliangshan on 17/2/13.
 * 图片加载工具类 —— 基于 Glide
 */
public class GlideImageLoaderUtil {
    public static final String TAG = "TZImageLoaderUtil";
    /**
     * 异步加载图片
     * @param url 图片路径
     */
    public static void loadImage(final ImageView imageView, String url) {
        loadImage(imageView, url, 0, null);
    }
    /**
     * 异步加载图片
     * @param url 图片路径
     * @param drawableId 占位图
     */
    public static void loadImage(final ImageView imageView, String url, int drawableId) {
        loadImage(imageView, url, drawableId, null);
    }

    /**
     * 异步加载图片, 带回调
     * @param url 图片路径
     * @param drawableId 占位图
     * @param callback 图片加载完成回调
     */
    public static void loadImage(final ImageView imageView, final String url, int drawableId, final Callback callback) {
        if(imageView == null) {
            Log.e(TAG, "loadImage() -> imageView is null");
            return;
        }

        Context context = imageView.getContext();
        if(!isValid(context))
            return ;

        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(drawableId)
                .error(drawableId)
                .into(new BitmapImageViewTarget(imageView){
                    /**
                     * <a href="https://github.com/bumptech/glide/issues/1764">onLoadFailed run some times</a>
                     */
                    boolean hasFailed = false;

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        if (callback != null) {
                            callback.onLoadingStarted(url, imageView);
                        }
                    }

                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        super.onResourceReady(bitmap, glideAnimation);
                        if (callback != null) {
                            callback.onLoadingComplete(url, imageView, bitmap);
                        }
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        if(hasFailed)
                            return;
                        hasFailed = true;

                        if(callback != null){
                            callback.onLoadingFailed(url, imageView);
                        }
                    }

                    @Override
                    public void onLoadCleared(Drawable placeholder) {
                        super.onLoadCleared(placeholder);
                        if(callback != null){
                            callback.onLoadingCancelled(url, imageView);
                        }
                    }
                });
    }

    /**
     * 异步加载图片, 带回调 (不缓存图片)
     * @param url 图片路径
     * @param drawableId 占位图
     * @param callback 图片加载完成回调
     */
    public static void loadImageWithoutMemoryCache(final ImageView imageView, final String url, int drawableId, final Callback callback) {
        if(imageView == null) {
            Log.e(TAG, "loadImage() -> imageView is null");
            return;
        }

        Context context = imageView.getContext();
        if(!isValid(context))
            return ;

        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(drawableId)
                .error(drawableId)
                .skipMemoryCache(true)
                .into(new BitmapImageViewTarget(imageView){
                    boolean hasFailed = false;

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        if (callback != null) {
                            callback.onLoadingStarted(url, imageView);
                        }
                    }

                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        super.onResourceReady(bitmap, glideAnimation);
                        if (callback != null) {
                            callback.onLoadingComplete(url, imageView, bitmap);
                        }
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        if(hasFailed)
                            return;
                        hasFailed = true;

                        if(callback != null){
                            callback.onLoadingFailed(url, imageView);
                        }
                    }

                    @Override
                    public void onLoadCleared(Drawable placeholder) {
                        super.onLoadCleared(placeholder);
                        if(callback != null){
                            callback.onLoadingCancelled(url, imageView);
                        }
                    }
                });
    }

    /**
     * 异步加载圆角图片, 使用 Glide 内置的圆角裁剪 —— bitmapTransform.
     *
     * 注意: bitmapTransform 与 外在的裁剪不兼容。即: https://github.com/wasabeef/glide-transformations/issues/54
     *
     *      1. 此时的 imageView 不能是自定义的圆角View.
     *      2. scaleType 也不能设。
     *
     * 否则重复裁剪, 会有 bug.
     *
     * @param url 图片路径
     * @param drawableId 占位图
     * @param radiusPixels 圆角像素值
     */
    public static void loadRoundImage(ImageView imageView, String url, int drawableId, int radiusPixels) {
        if(imageView == null) {
            Log.e(TAG, "loadRoundImage() -> imageView is null");
            return;
        }

        Context context = imageView.getContext();
        if(!isValid(context))
            return ;

        Glide.with(context)
                .load(url)
                .placeholder(drawableId)
                .error(drawableId)
                .bitmapTransform(
                        new CenterCrop(context),
                        new RoundedCornersTransformation(context, radiusPixels, 0)
                )
                .crossFade()
                .into(imageView);
    }

    /**
     * 异步加载图片, 带回调(不涉及 ImageView)
     * @param url 图片路径
     * @param callback 图片加载完成回调
     */
    public static void loadImage(Context context, final String url, final Callback callback) {
        if(!isValid(context))
            return ;

        Glide.with(context)
                .load(url)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>(){
                    boolean hasFailed = false;

                    @Override
                    public void onLoadStarted(Drawable placeholder) {
                        if (callback != null) {
                            callback.onLoadingStarted(url, null);
                        }
                    }

                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        if (callback != null) {
                            callback.onLoadingComplete(url, null, bitmap);
                        }
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        if(hasFailed)
                            return;
                        hasFailed = true;

                        if(callback != null){
                            callback.onLoadingFailed(url, null);
                        }
                    }

                    @Override
                    public void onLoadCleared(Drawable placeholder) {
                        if(callback != null){
                            callback.onLoadingCancelled(url, null);
                        }
                    }
                });
    }

    /**
     * <a href="https://github.com/bumptech/glide/issues/138">Issue #138: Getting a crash in Glide 3.3 library<a/>
     *
     * reason: {@link RequestManagerRetriever#assertNotDestroyed(Activity)}
     */
    private static boolean isValid(Context context){
        if(context instanceof Activity){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && ((Activity)context).isDestroyed())
                return false;
        }

        return true;
    }

    /**
     * 加载图片生命周期的回调
     */
    public static class Callback {
        protected void onLoadingStarted(String url, View imageView){}

        protected void onLoadingComplete(String url, View imageView, Bitmap bitmap){}

        protected void onLoadingFailed(String url, View imageView){}

        protected void onLoadingCancelled(String url, View imageView){}
    }
}
